package com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.repository.impl;

import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.entity.StructureSetting;
import com.css.fxfzypg.modules.auxiliaryDecisionManage.structureSetting.repository.StructureSettingRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/03/22
 * @Description: 结构类型设置
 */
@Repository
public class StructureSettingRepositoryImpl implements StructureSettingRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public StructureSetting getParamSetting(String id) {
        String sql = "select * from ypg_structure_setting where id ='"+id+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(StructureSetting.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public void save(StructureSetting structureSetting) {
        String sql = "insert into ypg_structure_setting (id,structure_type) " +
                "values(:id,:structureType)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(structureSetting);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void updateById(StructureSetting structureSetting) {
        String sql = "update ypg_structure_setting set structure_type = :structureType where id = '"+structureSetting.getId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(structureSetting);
        namedParameterJdbcTemplate.update(sql, source);
    }
}
